package cn.piesat.scanning.service.impl;

import cn.piesat.scanning.dao.DbDmsAlgorithmResultStoreDAO;
import cn.piesat.scanning.dto.DbDmsAlgorithmResultStoreDTO;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.DbDmsAlgorithmResultStoreService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 专业处理结果数据服务层接口方法的实现类
 * @author xhl
 */
@Service
public class DbDmsAlgorithmResultStoreServiceImpl implements DbDmsAlgorithmResultStoreService {

    @Autowired
    private DbDmsAlgorithmResultStoreDAO dbDmsAlgorithmResultStoreDAO;

    /**
     * 动态查询结果数据信息，当传入的某个参数值为空时，不添加该筛选条件。
     * @param satellite 卫星
     * @param senor 传感器
     * @param resolutions 分辨率
     * @param fileType 文件类型
     * @return
     */
    @Override
    public List<DbDmsAlgorithmResultStoreDTO> specificationList(String satellite, String senor, String resolutions,
                                                                String fileType, String nearDate) {
        return dbDmsAlgorithmResultStoreDAO.findAll((root,query,cb)-> {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(satellite)) {
                    predicates.add(cb.equal(root.get("satellite").as(String.class), satellite));
                }
                if (StringUtils.isNotBlank(senor)) {
                    predicates.add(cb.equal(root.get("senor").as(String.class), senor));
                }
                if (StringUtils.isNotBlank(resolutions)) {
                    predicates.add(cb.equal(root.get("resolution").as(String.class), resolutions));
                }
                if (StringUtils.isNotBlank(fileType)) {
                    predicates.add(cb.equal(root.get("filetype").as(String.class), fileType));
                }
                if (StringUtils.isNotEmpty(nearDate)) {
                    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                    String date = dtf.format(LocalDateTime.of(LocalDate.now().plusDays(-(Integer.valueOf(nearDate) - 1)),LocalTime.MIN));
                    predicates.add(cb.greaterThanOrEqualTo(root.get("fileDate").as(String.class),date));
                }
                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        });
    }

    /**
     * 根据数据等级集合查询day天之前的结果数据信息集合
     * @param day 天数
     * @param levels 数据等级结合
     * @return
     */
    @Override
    public List<DbDmsAlgorithmResultStoreDTO> findByBeforeFileDateAndLevels(Integer day, List<String> levels) {
        LocalDate now = LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String limitTime = dtf.format(LocalDateTime.of(now.plusDays(-1L * (day - 1)), LocalTime.MIN));
        return dbDmsAlgorithmResultStoreDAO.findByFileDateBeforeAndDataLevelIn(limitTime,levels);
    }

    /**
     * 根据结果数据信息ID删除数据库中的信息
     * @param id
     */
    @Override
    public void removeById(String id) {
        try{
            dbDmsAlgorithmResultStoreDAO.deleteById(id);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据文件名称查询结果数据信息
     * @param fileName 文件名称
     * @return
     */
    @Override
    public DbDmsAlgorithmResultStoreDTO findByFileName(String fileName) {
        return dbDmsAlgorithmResultStoreDAO.findByFilename(fileName);
    }

    @Override
    public List<DbDmsAlgorithmResultStoreDTO> regexAndDateRangeTime(String date, String fileNamePattern) {
        return dbDmsAlgorithmResultStoreDAO.regexAndDateRangeFind(date,fileNamePattern);
    }

    @Override
    public void deleteOne(DbDmsAlgorithmResultStoreDTO dto) {
        dbDmsAlgorithmResultStoreDAO.delete(dto);
    }

    @Override
    public List<DbDmsAlgorithmResultStoreDTO> findBeforeDayAndLevelListData(Integer day, List<String> resultDataLevelList) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        long plus = -1 * (day - 1);
        LocalDate now = LocalDate.now();
        String limitTime = dtf.format(LocalDateTime.of(now.plusDays(plus),LocalTime.MIN));
        return dbDmsAlgorithmResultStoreDAO.findByFileDateBeforeAndDataLevelIn(limitTime,resultDataLevelList);
    }

    @Override
    public List<DbDmsAlgorithmResultStoreDTO> findAfterDayAndLevelListData(Integer day, List<String> resultDataLevelList) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        long plus = -1 * day;
        LocalDate now = LocalDate.now();
        String limitTime = dtf.format(LocalDateTime.of(now.plusDays(plus),LocalTime.MAX));
        return dbDmsAlgorithmResultStoreDAO.findByFileDateAfterAndDataLevelIn(limitTime,resultDataLevelList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByTaskPlanIdIn(List<String> taskPlanIds) {
        dbDmsAlgorithmResultStoreDAO.deleteByTaskPlanIdIn(taskPlanIds);
    }

    @Override
    public List<DbDmsAlgorithmResultStoreDTO> listByProductIdentifyListAndTimeRangeAndLevel(List<String> asList, String startTime, String endTime, String level) {
        return dbDmsAlgorithmResultStoreDAO.findByDataLevelAndProductIdentifyInAndFileDateBetween(level,asList,startTime,endTime);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByFileDateIn(List<String> deleteFileDates) {
        dbDmsAlgorithmResultStoreDAO.deleteByFileDateIn(deleteFileDates);
    }

    @Override
    public List<DbDmsAlgorithmResultStoreDTO> findByDataLevelAndProductIdentifyInAndFileDateBetweenAndFiletype(List<String> asList, String startTime, String endTime, String level, String fileType) {
        return dbDmsAlgorithmResultStoreDAO.findByDataLevelAndProductIdentifyInAndFileDateBetweenAndFiletype(level,asList,startTime,endTime,fileType);
    }

    @Override
    public List<DbDmsAlgorithmResultStoreDTO> findByFileCatalogAndFiletypeAndFileDate(String fileCatalog, String filetype, String fileDate) {
        return dbDmsAlgorithmResultStoreDAO.findByFileCatalogAndFiletypeAndFileDate(fileCatalog,filetype,fileDate);
    }

    @Override
    public List<DbDmsAlgorithmResultStoreDTO> findByFileCatalogAndFiletypeAndFileDateBetween(String fileCatalog, String filetype,String leftTime,String rightTime) {
        return dbDmsAlgorithmResultStoreDAO.findByFileCatalogAndFiletypeAndFileDateBetween(fileCatalog,filetype,leftTime,rightTime);
    }


}
